package com.entity

import org.jsoup.Jsoup

class VersionJs {
    var jsName: String? = null

    var version: Int = 0

    var isChange: Boolean = false

    constructor() : super()

    constructor(jsName: String?) : super() {
        this.jsName = jsName
    }

    constructor(jsName: String?, version: Int, isChange: Boolean) : super() {
        this.jsName = jsName
        this.version = version
        this.isChange = isChange
    }

    val nextVersion: String
        get() {
            var str = ""
            if (version > 0) {
                str = "?v=" + (version + 1)
            }
            return str
        }

    val verStr: String
        get() {
            var str = ""
            str = if (version > 0) {
                "?v=$version"
            } else {
                "?v=1"
            }
            return str
        }

    val jsMinName: String
        get() = jsName!!.replace(".js".toRegex(), ".min.js")

    fun parseVersion(str: String) {
        val doc = Jsoup.parse(str)
        val scripts = doc.select("script[src]")
        var url: String
        val ind: Int
        for (e in scripts) {
            url = e.attr("src")
            url = url.replace(".min.js".toRegex(), ".js")
            if (url.contains("$jsName?v=")) {
                ind = url.indexOf("v=")
                url = url.substring(ind + 2)
                version = url.toInt()
                break
            }
        }
    }

    override fun toString(): String {
        return "VersionJs [jsName=$jsName, version=$version, isChange=$isChange]"
    }
}
